<%-
	require "luci.sys"
	require "luci.tools.webadmin"
	require "nixio.fs"
	require "luci.controller.idm"

	local style = true

	function get_status(callback)
		local text = status

		while text do
			pos = text:find('\n')
			if not pos then
				callback({line=text})
				text = nil
			else
				callback({line=text:sub(1,pos-1)})
				text = text:sub(pos+1)
			end
		end
	end
-%>

<%+header%>

<%if init_state == "refresh" then%>
<script>setTimeout('document.getElementById("init_state").value="refresh";document.getElementById("idm").submit()', 1000)</script>
<%end%>

<form name="idm" id="idm" method="post" action="<%=REQUEST_URI%>">
	<div>
		<input type="hidden" id="init_state" name="init_state" value="none" />
		<%if not (init_state == "none") then%>
		<input type="hidden" name="dns_domain" value="<%=dns_domain%>" />
		<input type="hidden" name="domain" value="<%=domain%>" />
		<input type="hidden" name="basedn" value="<%=basedn%>" />
		<input type="hidden" name="ldappw" value="<%=ldappw%>" />
		<%end%>
	</div>
	<div class="cbi-map" id="cbi-idm"><fieldset class="cbi-section" id="cbi-idm-table">
		<table class="cbi-section-table">
			<tr class="cbi-section-table-titles">
				<th class="cbi-section-table-cell left" style="width: 20%"><%:DNS domain%></th>
				<th class="cbi-section-table-cell left" style="width: 20%"><%:Domain%></th>
				<th class="cbi-section-table-cell left" style="width: 20%"><%:LDAP base DN%></th>
				<th class="cbi-section-table-cell left" style="width: 20%"><%:LDAP Manager password%></th>
				<th class="cbi-section-table-cell left" style="width: 20%"></th>
			</tr>
			<tr class="cbi-section-table-row">
			<%if init_state == "none" then%>
				<td class="cbi-section-table-cell left">
					<input class="cbi-input-text" type="text" name="dns_domain" value="<%=dns_domain%>"
						onkeyup="document.getElementById('domain').value=this.value;document.getElementById('basedn').value='dc='+this.value.replace(/\./g,',dc=');" />
				</td>
				<td class="cbi-section-table-cell left">
					<input class="cbi-input-text" type="text" name="domain" id="domain" value="<%=domain%>"
						style="text-transform: uppercase" />
				</td>
				<td class="cbi-section-table-cell left">
					<input class="cbi-input-text" type="text" name="basedn" id="basedn" value="<%=basedn%>" />
				</td>
				<td class="cbi-section-table-cell left">
					<input class="cbi-input-password" type="text" name="ldappw" id="ldappw" value="<%=ldappw%>" />
				</td>
			<%else%>
				<td class="cbi-section-table-cell left"><%=dns_domain%></td>
				<td class="cbi-section-table-cell left"><%=domain%></td>
				<td class="cbi-section-table-cell left"><%=basedn%></td>
				<td class="cbi-section-table-cell left">
					<div style="text-indent: 17px; background: url('<%=resource%>/cbi/key.gif') no-repeat scroll 1px center;"><%=ldappw%></div>
				</td>
			<%end%>
			</tr>
			<%if init_state == "none" then%><tr class="cbi-section-table-row">
				<td class="cbi-section-table-cell left" colspan="5">
					<input class="cbi-button cbi-button-apply" type="submit" value="<%:Start%>"
						onclick="document.getElementById('init_state').value='run'" />
				</td>				
			</tr>
			<%if nixio.fs.access("/var/run/ldapi") then %><tr class="cbi-section-table-row">
				<td class="cbi-section-table-cell left" colspan="5">
					<div style="text-indent: 17px; background: url('<%=resource%>/cbi/reset.gif') no-repeat scroll 1px center;"><%:Warning! Initializing IDM will erase all existing objects!%></div>
				</td>				
			</tr><%end%><%end%>
		</table>
	</fieldset></div>
</form>
<%if init_state == "refresh" or init_state == "finish" then%>
<div class="cbi-map" id="cbi-group"><fieldset class="cbi-section" id="cbi-table-status">
	<table class="cbi-section-table">
		<% style = nil; newgid = 1000;  get_status(function(item) %>
		<tr class="cbi-section-table-row cbi-rowstyle-<%=(style and 1 or 2)%>">
			<td class="cbi-value-field" style="text-align:left"><%=item.line%></td>
		</tr>
		<% style = not style; end) %>
	</table>
</fieldset></div>
<%end%>

<%+footer%>
